﻿using Cinch;
using CodeSHARPer.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;

namespace CodeSHARPer.ViewModel.Dialogs
{
    public class FrameworkAssembliesViewModel : ViewModelBase
    {
        string filter;
        public string Filter
        {
            get { return filter; }
            set
            {
                filter = value;
                if (string.IsNullOrEmpty(filter))
                    UsedReferences = new ObservableCollection<References>(referencesBackup);
                else
                {
                    var filteredReferences =
                        from r in referencesBackup
                        where r.Name.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0
                        select r;

                    UsedReferences = new ObservableCollection<References>(filteredReferences);
                }
                NotifyPropertyChanged(ObservableHelper.CreateArgs<FrameworkAssembliesViewModel>(x => x.Filter));
            }
        }

        BackgroundWorker backgroundWorker;
        List<References> referencesBackup;

        ObservableCollection<References> references;
        public ObservableCollection<References> UsedReferences
        {
            get
            {
                if (references == null)
                {
                    backgroundWorker = new BackgroundWorker();
                    backgroundWorker.DoWork += backgroundWorker_DoWork;
                    backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
                    backgroundWorker.WorkerReportsProgress = true;
                    backgroundWorker.RunWorkerAsync();

                    references = new ObservableCollection<References>();
                }
                return references;
            }
            set
            {
                references = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<FrameworkAssembliesViewModel>(x => x.UsedReferences));
            }
        }

        void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            References reference = e.UserState as References;
            this.UsedReferences.Add(reference);
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            referencesBackup = new List<References>();
            //get the folder
            DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(typeof(object).Assembly.Location));
            foreach (FileInfo file in di.EnumerateFiles("*.dll"))
            {
                References reference = new References(file.FullName);
                if (reference.HasError == false)
                {
                    referencesBackup.Add(reference);
                    backgroundWorker.ReportProgress(0, reference);
                }
            }
        }
    }
}
